import 'package:appflutter/day11/Provider/Person_view_model.dart';
import 'package:appflutter/day11/Provider/Providers.dart';
import 'package:appflutter/day11/Provider/view_model.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

main() {

  runApp(
    MultiProvider(
      providers: providers,
      child: myApp(),
    )
  
  );
}

class myApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      theme: ThemeData(primaryColor: Colors.blue),
      home: MyHomePage(),
    );
  }
}

class MyHomePage extends StatefulWidget {
  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('首页'),
        elevation: 0,
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            demo1(),
            SizedBox(
              height: 8,
            ),
            demo2(),
            person_demo()
          ],
        ),
      ),
      floatingActionButton: Selector<NWCounter_View_model,NWCounter_View_model>(
        //build方法
        builder: (context,counter_view_model,child){

          print('FloatingActionButton被创建');
          return FloatingActionButton(
            onPressed: () {
              counter_view_model.counter += 1;
            },
            child: child,
          );
        },
        //是否需要方法替换(不需要,返回本身)
        selector: (context,provider) => provider,
        //是否需要重建
        shouldRebuild:(pre,next) =>false,

        child: Icon(Icons.add),
      ),

    );
  }
}

class demo1 extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Container(
        color: Colors.red,
        child: Text(
          '当前计数:100',
          style: TextStyle(fontSize: 20, color: Colors.white),
        ));
  }
}

class demo2 extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Container(
        color: Colors.blue,
        child: Consumer<NWCounter_View_model>(
          builder: (context,view_model,child){
            return Text(
              '当前计数:${view_model.counter}',
              style: TextStyle(fontSize: 20, color: Colors.white),
            );
          },
        )

    );
  }
}

class person_demo extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Consumer2<NWCounter_View_model,NWPerson_view_model>(builder: (contect ,counter_view_model,person_view_model,child){
      
      return Text('nickname: ${person_view_model.person_model.name} age: ${counter_view_model.counter}',style: TextStyle(fontSize: 20),);
    });
  }
}
